﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static App.Untiys.Enum;

namespace App.Untiys
{
    public class FormHelper
    {

        /// <summary>
        ///  委托
        /// </summary>
        /// <param name="ricMsg"></param>
        /// <param name="type"></param>
        /// <param name="_msg"></param>
        private delegate void ApplicationInfomation(UIRichTextBox ricMsg, MsgType type, string _msg);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        public static void RicMsgShow(UIRichTextBox ricMsg, MsgType _type, string _message)
        {
            try
            {
                //在线程里以安全方式调用控件
                if (ricMsg.InvokeRequired)
                {
                    ApplicationInfomation _MyInvoke = new ApplicationInfomation(RicMsgShow);
                    ricMsg.Invoke(_MyInvoke, new object[] { ricMsg, _type, _message });
                }
                else
                {
                    Color co;
                    switch (_type)
                    {
                        case MsgType.Error:
                            co = Color.Red;
                            break;
                        case MsgType.Waring:
                            co = Color.Blue;
                            break;
                        default:
                            co = Color.Green;
                            break;
                    }
                    int len = ricMsg.TextLength;
                    ricMsg.Select(len, 0);
                    ricMsg.SelectionColor = co;
                    ricMsg.ScrollToCaret();//让滚动条拉到最底处               
                    ricMsg.AppendText(DateTime.Now.ToString("HH:mm:ss") + "  " + _message + "\r\n");
                    LogHelper.DoWriteLogTypeSubitem(LogType.Program, LogSubitem.Log, _message);
                }
            }
            catch
            {

            }
        }
    }
}
